package com.jie.lipassword;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.jie.lipassword.db.DbContect;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class PasswordListActivity extends AppCompatActivity {
    private RelativeLayout password_list_title;
    private ImageButton password_add;
    private ListView password_list_view;


    private String type_name;
    private DbContect helper;
    private SQLiteDatabase db;
    private ArrayList<String> password_list_data = new ArrayList<String>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_list);
        type_name = getIntent().getStringExtra("type_name");
        dbcontext();
    }

    @Override
    protected void onResume() {
        super.onResume();
        set_init();
        showList();
    }



    private void set_init() {
        password_list_title = findViewById(R.id.password_list_title);
        password_add = findViewById(R.id.password_add);
        password_list_view = findViewById(R.id.password_list_view);

        FindTitle.setTitle(this,password_list_title,true,false,type_name);
        password_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PasswordListActivity.this,PasswordAddActivity.class);
                intent.putExtra("type_name",type_name);
                startActivity(intent);
            }
        });
    }

    private void dbcontext(){
        helper = new DbContect(this);
        db = helper.getWritableDatabase();
    }

    private void showList() {
        Cursor cursor = db.query("PasswordParameters",null,"type_name=?",new String[]{type_name},null,null,null);
        password_list_data = new ArrayList<>();
        while(cursor.moveToNext()){
            @SuppressLint("Range") String account = cursor.getString(cursor.getColumnIndex("account"));
            password_list_data.add(account);
            Log.i("PasswordAdd","result: account: " + account);
        }
        // 关闭游标，释放资源
        cursor.close();

        List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
        for(int i = 0; i < password_list_data .size(); i++){
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("account", "账号:"+password_list_data.get(i));
            data.add(item);
        }
        SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.password_item,new String[]{"account"},new int[]{R.id.item_account});
        password_list_view.setAdapter(adapter);
        password_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(PasswordListActivity.this,password_list_data.get(i), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(PasswordListActivity.this,AccountPasswordActivity.class);
                intent.putExtra("type_name",type_name);
                intent.putExtra("account",password_list_data.get(i));
                startActivity(intent);
            }
        });
    }
}
